#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
# pylint: disable=redefined-outer-name, unused-argument
from TTS.utils.synthesizer import Synthesizer

class SpeechSynthesizer:
    def __init__(self) -> None:
        self.model_dir = os.path.expanduser("~")+"/Lepi_Data/ros/tts/tts_models--zh-CN--baker--tacotron2-DDC-GST/"
        self.model_path = os.path.join(self.model_dir,"model_file.pth")
        self.config_path = os.path.join(self.model_dir,"config.json")
        self.speakers_file_path = None
        self.language_ids_file_path = None
        self.vocoder_dir = os.path.expanduser("~")+"/Lepi_Data/ros/tts/vocoder_models--universal--libri-tts--fullband-melgan/"
        self.vocoder_path = os.path.join(self.vocoder_dir,"model_file.pth")
        self.vocoder_config_path = os.path.join(self.vocoder_dir,"config.json")
        self.encoder_path = None
        self.encoder_config_path = None
        self.out_path = "/tmp/out.wav"
        # load models
        self.synthesizer = Synthesizer(
            self.model_path,
            self.config_path,
            self.speakers_file_path,
            self.language_ids_file_path,
            self.vocoder_path,
            self.vocoder_config_path,
            self.encoder_path,
            self.encoder_config_path,
        )

    def tts(self,text):
        # kick it
        wav = self.synthesizer.tts(
            text,
            None,
            None,
            None,
            reference_wav=None,
            reference_speaker_name=None,
        )

        # save the results
        print(" > Saving output to {}".format(self.out_path))
        self.synthesizer.save_wav(wav, self.out_path)


if __name__ == "__main__":
    synthesizer = SpeechSynthesizer()
    synthesizer.tts("中文可以的亲。")
